/**
  ******************************************************************************
  * File Name          : weight.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "xc.h"
#include "route_expio_485.h"
#include "storage.h"
#include <string.h>
#include <stdlib.h>

const Route_DevType_Type Route_LocalDevType = RT_DV_XC;

static uint8_t  Xc_OutTime_Last;
static uint8_t  Xc_CurrentPosition_Last;
static uint16_t Xc_UpLimit_Last;

static void route_subdev_xc_updata(Route_PayloadType *outpl, uint8_t forcelevel)
{
  if((forcelevel != 0) || \
     (Xc_OutTime_Last != Xc_OutTime) || \
     (Xc_CurrentPosition_Last != Xc_CurrentPosition) || \
     (Xc_UpLimit_Last != Xc_UpLimit))
  {
    Xc_OutTime_Last = Xc_OutTime;
    Xc_CurrentPosition_Last = Xc_CurrentPosition;
    Xc_UpLimit_Last = Xc_UpLimit;
    
    ((Xc_UpParaType*)(outpl->payload))->curt_pos = Xc_CurrentPosition;
    ((Xc_UpParaType*)(outpl->payload))->out_time = Xc_OutTime;
    ((Xc_UpParaType*)(outpl->payload))->up_limit = Xc_UpLimit;
    ((Xc_UpParaType*)(outpl->payload))->curt_cod = Xc_GetPosition();
    
    outpl->trans_dir = ROUTE_TRSDIR_SUBDEV_TO_NET;
    outpl->pl_type = ROUTE_PLTYP_XC_UPDTLV1;
    outpl->pl_len  = sizeof(Xc_UpParaType);
  }
  else
  {
    outpl->trans_dir = ROUTE_TRSDIR_SUBDEV_TO_LOCAL;
    outpl->pl_type   = ROUTE_PLTYP_XC_UPDTLV0;
    outpl->pl_len    = 0;
  }
}

uint8_t Route_expio_xx_rxprocess(Route_PayloadType *inpl, Route_PayloadType *outpl)
{
  uint8_t forcelevel = 0;
  Xc_ActionCmdType scmd;
  
  switch(inpl->pl_type)
  {
    case ROUTE_PLTYP_XC_CYCLE:
      break;
    
    case ROUTE_PLTYP_XC_SYNC:
      forcelevel = 1;
      break;
    
    case ROUTE_PLTYP_XC_RESET:
      if(inpl->pl_len != 0)
      {
        return 1;
      }
      
      if(Xc_RxCmd_Qhd != NULL)
      {
        xQueueSend(Xc_RxCmd_Qhd, &Xc_CmdReset, 0);
      }
      break;
    
    case ROUTE_PLTYP_XC_SETPARA:
      if(inpl->pl_len != sizeof(Xc_SetParaType))
      {
        return 1;
      }
      
      if(((Xc_SetParaType *)(inpl->payload))->curt_pos != Xc_CurrentPosition)
      {
        if(((Xc_SetParaType *)(inpl->payload))->curt_pos <= 101)
        {
          if(Xc_RxCmd_Qhd != NULL)
          {
            scmd.cmd = XC_ACTCMD_ACTION;
            scmd.dat = ((Xc_SetParaType *)(inpl->payload))->curt_pos;
            xQueueSend(Xc_RxCmd_Qhd, &scmd, 0);
          }
        }
      }
      
      if(((Xc_SetParaType *)(inpl->payload))->out_time != Xc_OutTime)
      {
        if(Xc_CheckOutTime(((Xc_SetParaType *)(inpl->payload))->out_time) == 0)
        {
          Xc_OutTime = ((Xc_SetParaType *)(inpl->payload))->out_time;
          
          if(Storage_qhd != 0)
          {
            xQueueSend(Storage_qhd, &Xc_SaveOutTime, 0);
          }
        }
        forcelevel = 1;
      }
      
      break;

    default:
      return 3;
  }
  
  if(outpl != NULL)
  {
    route_subdev_xc_updata(outpl, forcelevel);
  }
  else
  {
    return 4;
  }
  
  return 0;
}
